﻿/**************************************************************************
创建日期:	2019/6/10  
作	  者:	张存
邮 	  箱:	zhangcunliang@126.com
创建时间:	
Copyright (c)  ZhCun.cn

描	述：
记	录：2019.10.25 增加可动态更改配置文件的实现，默认为 appsettings.json
        以后参考源码可再优化配置文件的实现
***************************************************************************/
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Linq;

namespace ZhCun.Utils
{
    public class ConfigHelper
    {
        /// <summary>
        /// 配置文件是否发生了变化(AppConfigBase静态类会用到，不要去掉)
        /// </summary>
        internal static bool ConfigFileChanged { get; set; }

        static IConfigurationRoot ConfigRoot { set; get; }

        static JsonConfigurationProvider CurrConfigProvider { set; get; }

        static ConfigHelper()
        {
            LoadConfigFile();
            SetConfigFile();
        }

        public static string CurrConfigFile
        {
            get
            {
                return CurrConfigProvider?.Source.Path;
            }
        }

        public static void LoadConfigFile()
        {
            string currDir = Directory.GetCurrentDirectory();            
            var configFiles = Directory.GetFiles(currDir, "*.json");
            var config = new ConfigurationBuilder();
            foreach (var item in configFiles)
            {
                var fileName = Path.GetFileName(item);
                if (fileName.StartsWith("appsettings."))
                    config.Add(new JsonConfigurationSource { Path = fileName, ReloadOnChange = true });
            }            
            ConfigRoot = config.Build();
        }

        public static bool SetConfigFile(string fileName = "appsettings.json", bool isReloadConfig = false)
        {
            if (CurrConfigFile != null && CurrConfigFile.Equals(fileName, StringComparison.CurrentCultureIgnoreCase))
            {
                //配置文件相同，不做任何改变
                return false;
            }

            if (isReloadConfig)
            {
                LoadConfigFile();
                ConfigRoot.Reload();
            }

            foreach (var item in ConfigRoot.Providers)
            {
                string currConfigFile = ((JsonConfigurationProvider)item).Source.Path;
                if (currConfigFile.Equals(fileName, StringComparison.CurrentCultureIgnoreCase))
                {
                    CurrConfigProvider = ((JsonConfigurationProvider)item);
                    ConfigFileChanged = true;
                    return true;
                }
            }
            return false;
        }

        //public static IConfigurationProvider GetConfigProvider(string fileName)
        //{
        //    foreach (var item in ConfigRoot.Providers)
        //    {
        //        string currConfigFile = ((JsonConfigurationProvider)item).Source.Path;
        //        if (currConfigFile.Equals(fileName, StringComparison.CurrentCultureIgnoreCase))
        //        {
        //            CurrConfigProvider = item;
        //            ConfigFileChanged = true;
        //            return true;
        //        }
        //    }
        //    return false;
        //}

        public static string GetConnectString(string name)
        {
            CurrConfigProvider.TryGet($"ConnectionStrings:{name}", out string val);
            return val;
        }

        public static string GetAppSettings(string key)
        {
            CurrConfigProvider.TryGet($"Appsettings:{key}", out string val);
            return val;
        }

        public static string GetConfigProvider(string key)
        {
            CurrConfigProvider.TryGet(key, out string val);
            return val;
        }
    }
}